package com.tengju.bff.interfaces.app.archive.assembler;

import com.tengju.bff.interfaces.app.archive.vo.ResumeVO;
import com.tengju.user.domain.model.archive.resume.*;
import com.tengju.user.domain.model.user.UserId;
import com.tengju.user.domain.shared.IdObject;
import com.tengju.user.domain.shared.JsonMap;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
public class ArchiveAssembler {

    private ArchiveAssembler() {
    }

    public static ResumeVO modelToVo(Resume resume) {
        return resume == null ? null : ResumeVO.builder()
                .resumeId(resume.getResumeId().getValue())
                .brands(
                        Optional.ofNullable(resume.getBrandIds())
                                .map(ids -> ids.stream()
                                        .map(IdObject::getValue)
                                        .collect(Collectors.toSet())
                                )
                                .orElse(new HashSet<>())
                )
                .selfMedia(
                        Optional.ofNullable(resume.getSelfMedia())
                                .map(JsonMap::getData)
                                .orElse(null)
                )
                .community(
                        Optional.ofNullable(resume.getCommunity())
                                .orElse(null)
                )
                .companyInfo(Optional.ofNullable(resume.getCompany())
                        .orElse(null)
                )
                .channelExperience(resume.getChannelExperience())
                .build();
    }

    public static Resume voToModel(ResumeVO resumeVO, UserId userId, ResumeId resumeId) {
        return new Resume(
                resumeId,
                userId,
                Optional.ofNullable(resumeVO.getBrands())
                        .map(brands -> brands.stream()
                                .map(BrandId::new)
                                .collect(Collectors.toList())
                        ).orElse(null),
                Optional.ofNullable(resumeVO.getSelfMedia())
                        .map(ArchiveAssembler::objToMap)
                        .map((Function<Map<String, String>, JsonMap<SelfMediaType>>) JsonMap::new)
                        .orElse(null),
                Optional.ofNullable(resumeVO.getCommunity())
                        .map(communityVO -> new CommunityInfo(
                                        communityVO.getWechatId(),
                                        communityVO.getFriendCount(),
                                        communityVO.getVideoId(),
                                        communityVO.getGroupCount(),
                                        communityVO.getCoveredPeople()
                                )
                        )
                        .orElse(null)
                ,
                Optional.ofNullable(resumeVO.getCompanyInfo())
                        .map(companyInfoVO -> new CompanyInfo(
                                        companyInfoVO.getCompanyName(),
                                        companyInfoVO.getAddress(),
                                        companyInfoVO.getStaffCount()
                                )
                        )
                        .orElse(null)
                ,
                resumeVO.getChannelExperience(),
                null
        );
    }

    private static Map<String, String> objToMap(Object obj) {
        Map<String, String> map = new HashMap<>();
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                map.put(field.getName(), String.valueOf(field.get(obj)));
            } catch (IllegalAccessException e) {
                log.error("反射字段失败", e);
            }
        }
        return map;
    }
}


